App({
	//请不要修改 /addons/cms/wxapp.这部分,只允许修改域名部分
	//请注意小程序只支持https
	apiUrl: 'https://huishou.bigchun.com',
	cdnUrl: '',
	si: 0,
	//小程序启动
	onLaunch: function () {
		var that = this;
		var accountInfo = wx.getAccountInfoSync();
		var env = accountInfo.miniProgram.envVersion;
		if ( env=='develop' || env=='trial' ) {
			that.apiUrl = 'https://huishou.bigchun.com';
		}
		/* 全局获取窗口高度*/
		/* 获取窗口屏幕 */
		wx.getSystemInfo({
			success (res) {
				that.globalData.windowHeight = res.windowHeight;
				that.globalData.windowWidth = res.windowWidth;
			}
		});
		that.request('/addons/cms/wxapp.common/init', {}, function (data, ret) {
			that.globalData.config = data.config;
			that.cdnUrl = data.config.upload.cdnurl
			//如果需要一进入小程序就要求授权登录,可在这里发起调用
			if ( wx.getStorageSync("token") ) {
				// that.check(function (ret) {});
			}
		}, function (data, ret) {
			that.error(ret.msg);
		});
	},
	//判断是否登录
	check: function (cb, url) {
		
		var that = this;
		if ( that.globalData.userInfo ) {
			if ( that.globalData.userInfo.rider_apply ) {
				
			} else {
				wx.showModal({
					title: '',
					content: '你还不是平台骑手，无法进入平台',
					confirmText: "骑手申请",
					cancelText: "取消",
					cancelColor: "#5E6266",
					confirmColor: "#02B3BA",
					success: function (res) {
						if ( res.confirm ) {
							wx.getUserProfile({
								lang: 'zh',
								desc: '授权用户信息',
								success: function (res) {
									that.login(res.userInfo, function () {
									}, url);
								},
								fail: function (e) {
									that.info(JSON.stringify(e));
								}
							})
						} else {
							var page = getCurrentPages();
							if ( page.length>1 ) {
								wx.navigateBack({});
							} else {
								wx.reLaunch({
									url: '/page/index/index'
								});
							}
							console.log('用户暂不登录');
						}
					}
				});
			}
			typeof cb=="function" && cb(that.globalData.userInfo);
		} else {
			that.login({}, function () {
				that.onShow();
			}, url)
		}
	},
	//登录
	login: function (userInfo, cb, url) {
		var that = this;
		var token = wx.getStorageSync('token') || '';
		//调用登录接口
		wx.login({
			success: function (res) {
				if ( res.code ) {
					//发起网络请求
					wx.request({
						url: that.apiUrl + '/api/timetable.user/login',
						data: {
							code: res.code,
							rawData: JSON.stringify(userInfo),
							token: token
						},
						method: 'post',
						header: {
							"Content-Type": "application/x-www-form-urlencoded"
						},
						success: function (lres) {
							var response = lres.data
							if ( response.code==1 ) {
								that.globalData.userInfo = response.data.userInfo;
								wx.setStorageSync('token', response.data.userInfo.token);
								// that.success('登录成功');
								// that.onload();
								if ( url ) {
									wx.reLaunch({
										url: url
									});
								}
								typeof cb=="function" && cb(that.globalData.userInfo);
							} else {
								wx.setStorageSync('token', '');
								console.log("用户登录失败")
								that.showLoginModal(cb, url);
							}
						}
					});
				} else {
					wx.hideToast({});
					that.showLoginModal(cb, url);
				}
			}
		});
	},
	showApply: function () {
		/* 判断是否为骑手 */
		var that = this;
		if ( !that.globalData.userInfo.rider_apply ) {
			wx.showModal({
				title: '',
				content: '你还不是平台骑手，无法进入平台',
				confirmText: "骑手申请",
				cancelText: "取消",
				cancelColor: "#5E6266",
				confirmColor: "#02B3BA",
				success: function (res) {
					if ( res.confirm ) {
						/* 跳转到申请骑手*/
						wx.reLaunch({
							url: '/page/rider_apply/step1'
						});
						return false;
					} else {
						return false;
					}
				}
			});
		} else {
			if ( that.globalData.userInfo.rider_apply.status=='0' ) {
				wx.showModal({
					title: '',
					content: '您的账号正在审核中\r\n请您耐心等待',
					showCancel: false,
					confirmText: "确定",
					cancelText: "取消",
					cancelColor: "#5E6266",
					confirmColor: "#02B3BA",
					success: function (res) {
						if ( res.confirm ) {
							/* 跳转到申请骑手*/
							wx.reLaunch({
								url: '/page/index/index'
							});
							return false;
						} else {
							return false;
						}
					}
				});
			}
			if ( that.globalData.userInfo.rider_apply.status=='2' ) {
				wx.showModal({
					title: '',
					content: '您的账号审核未通过\r\n请重新提交申请',
					confirmText: "重新提交",
					cancelText: "取消",
					cancelColor: "#5E6266",
					confirmColor: "#02B3BA",
					success: function (res) {
						if ( res.confirm ) {
							/* 跳转到申请骑手*/
							wx.reLaunch({
								url: '/page/rider_apply/step1'
							});
							return false;
						} else {
							wx.reLaunch({
								url: '/page/index/index'
							});
							return false;
						}
					}
				});
			}
		}
	},
	//显示登录或授权提示
	showLoginModal: function (cb, url) {
		var that = this;
		if ( !that.globalData.userInfo ) {
			//获取用户信息
			wx.showModal({
				title: '温馨提示',
				content: '当前无法获取到你的个人信息，部分操作可能受到限制',
				confirmText: "登录",
				cancelText: "暂不登录",
				success: function (res) {
					if ( res.confirm ) {
						wx.getUserProfile({
							lang: 'zh',
							desc: '授权用户信息',
							success: function (res) {
								that.login(res.userInfo, function () {
								}, url);
							},
							fail: function (e) {
								that.info(JSON.stringify(e));
							}
						})
					} else {
						var page = getCurrentPages();
						if ( page.length>1 ) {
							wx.navigateBack({});
						} else {
							wx.reLaunch({
								url: '/page/index/index'
							});
						}
						console.log('用户暂不登录');
					}
				}
			});
		} else {
			typeof cb=="function" && cb(that.globalData.userInfo);
		}
	},
	//发起网络请求
	request: function (url, data, success, error) {
		var that = this;
		if ( typeof data=='function' ) {
			success = data;
			error = success;
			data = {};
		}
		if ( this.globalData.userInfo ) {
			data['user_id'] = this.globalData.userInfo.id;
			data['token'] = this.globalData.userInfo.token;
		}
		//移除最前的/
		// while (url.charAt(0) === '/')
		//   url = url.slice(1);
		this.loading(true);
		let cookie = wx.getStorageSync('cookieKey');
		let header = {
			"Content-Type": "application/x-www-form-urlencoded"
		};
		if ( cookie ) {
			header.Cookie = cookie;
		}
		if ( this.globalData.__token__ ) {
			data.__token__ = this.globalData.__token__;
		}
		data._ajax = 1;
		wx.request({
			url: this.apiUrl + url,
			data: data,
			method: 'post',
			header: header,
			success: function (res) {
				that.loading(false);
				var code, msg, json;
				if ( res && res.header ) {
					if ( res.header['Set-Cookie'] ) {
						wx.setStorageSync('cookieKey', res.header['Set-Cookie']); //保存Cookie到Storage
					}
					if ( res.header['__token__'] ) {
						that.globalData.__token__ = res.header['__token__'];
					}
				}
				if ( res.statusCode===200 ) {
					json = res.data;
					if ( json.code===1 ) {
						typeof success==='function' && success(json.data, json);
					} else {
						typeof error==='function' && error(json.data, json);
					}
				} else {
					json = typeof res.data==='object' ?res.data:{
						code: 0,
						msg: '发生一个未知错误',
						data: null
					};
					typeof error==='function' && error(json.data, json);
				}
			},
			fail: function (res) {
				that.loading(false);
				console.log("fail:", res);
				typeof error==='function' && error(null, {
					code: 0,
					msg: '',
					data: null
				});
			}
		});
	},
	//构造CDN地址
	cdnurl: function (url) {
		return url.toString().match(/^https?:\/\/(.*)/i) ?url:this.globalData.config.upload.cdnurl + url;
	},
	//文本提示
	info: function (msg, cb) {
		wx.showToast({
			title: msg,
			icon: 'none',
			duration: 2000,
			complete: function () {
				typeof cb=="function" && cb();
			}
		});
	},
	//成功提示
	success: function (msg, cb) {
		wx.showToast({
			title: msg,
			icon: 'none',
			// image: '/assets/images/ok.png',
			duration: 2000,
			complete: function () {
				typeof cb=="function" && cb();
			}
		});
	},
	//错误提示
	error: function (msg, cb) {
		wx.showToast({
			title: msg,
			icon: 'none',
			// image: '/assets/images/error.png',
			duration: 2000,
			complete: function () {
				typeof cb=="function" && cb();
			}
		});
		return false;
	},
	//警告提示
	warning: function (msg, cb) {
		wx.showToast({
			title: msg,
			image: '/assets/images/warning.png',
			duration: 2000,
			complete: function () {
				typeof cb=="function" && cb();
			}
		});
	},
	//Loading
	loading: function (msg) {
		if ( typeof msg=='boolean' ) {
			if ( !msg ) {
				if ( !this.si ) {
					return;
				}
				clearTimeout(this.si);
				wx.hideLoading({});
				return;
			}
		}
		msg = typeof msg=='undefined' || typeof msg=='boolean' ?'加载中':msg;
		this.globalData.loading = true;
		if ( this.si ) {
			return;
		}
		this.si = setTimeout(function () {
			wx.showLoading({
				title: msg
			});
		}, 300);
		
	},
	//全局信息
	globalData: {
		userInfo: null,
		config: null,
		token: '',
		indexTabList: [],
		newsTabList: [],
		productTabList: [],
		windowHeight: ''
	}
})